package com.littlebee.storysuper.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.TextView;

import com.littlebee.storysuper.R;
import com.littlebee.storysuper.entry.User;
import com.littlebee.storysuper.fragment.RegisterFragment;
import com.littlebee.storysuper.utils.LogUtils;
import com.littlebee.storysuper.utils.NetWorkUtils;
import com.littlebee.storysuper.utils.ToastUtil;
import com.littlebee.storysuper.utils.Utils;
import com.littlebee.storysuper.widget.CircleImageView;
import com.littlebee.storysuper.zxing.CaptureActivity;
import com.zss.library.PermissionCallBack;
import com.zss.library.activity.BaseActivity;
import com.zss.library.toolbar.CommonToolbar;
import com.zss.library.utils.SharedPrefUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


/**
 * 主页
 *
 * 13916014463
 */
public class MainActivity extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
    /**
     * 主页的四个大控件啊
     */
    CircleImageView iv1, iv2, iv3, iv4;
    /**
     * 主页有可能用到下拉刷新
     */
    private SwipeRefreshLayout mSwipeLayout;
    private static final int REFRESH_COMPLETE = 0X110;


    @Override
    public int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        super.initView();
        iv1 = (CircleImageView) findViewById(R.id.photo1);
        iv2 = (CircleImageView) findViewById(R.id.photo2);
        iv3 = (CircleImageView) findViewById(R.id.photo3);
        iv4 = (CircleImageView) findViewById(R.id.photo4);

        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);

        mSwipeLayout.setOnRefreshListener(this);

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        iv1.setOnClickListener(this);
        iv2.setOnClickListener(this);
        iv3.setOnClickListener(this);
        iv4.setOnClickListener(this);
        verifyPermissions(MainActivity.this, Manifest.permission.RECORD_AUDIO,
                new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}
                , 0x01, new PermissionCallBack() {

                    @Override
                    public void onGranted() {

                    }

                    @Override
                    public void onDenied() {
                    }
                });
        getInfo();
    }

    @Override
    public void setTopBar() {
        super.setTopBar();
        setStatusBarColor(R.color.module_video_bg);
        CommonToolbar toolbar = getToolbar();
        toolbar.setBgRes(R.color.module_video_bg);
        toolbar.setTitle(getString(R.string.app_name));
        TextView textView = new TextView(this);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(16);
        textView.setText("    绑定");
        toolbar.setLeftView(textView);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2017/8/14

                if (Utils.isLogin()) {
                    Intent intent = new Intent(MainActivity.this, BindActivity.class);
                    startActivityForResult(intent, 0x01);

                } else {
                    Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                    startActivity(intent);
                }
            }


        });
        toolbar.setRightText("我的");
        toolbar.setOnRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2017/8/14
                if (Utils.isLogin()) {
                    Intent intent = new Intent(MainActivity.this, MeActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                    startActivity(intent);
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.photo1:
                if (Utils.isLogin()) {
                    intent = new Intent(MainActivity.this, TodayStoryActivity.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(MainActivity.this, RegisterActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.photo2:

                if (Utils.isLogin()) {
                    intent = new Intent(MainActivity.this, EnglishActivity.class);
                    intent.putExtra("type", 0);
                    startActivity(intent);
                } else {
                    intent = new Intent(MainActivity.this, RegisterActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.photo3:

                if (Utils.isLogin()) {
                    intent = new Intent(MainActivity.this, ChatActivity.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(MainActivity.this, RegisterActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.photo4:
                if (Utils.isLogin()) {
                    intent = new Intent(MainActivity.this, PhotoActivity.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(MainActivity.this, RegisterActivity.class);
                    startActivity(intent);
                }


                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x01 && resultCode == Activity.RESULT_OK) {
//            String result = data.getStringExtra("result");
//            // TODO: 2017/8/14
//            LogUtils.i("XHX","XHX返回："+result);

        }
    }

    @Override
    public void onRefresh() {
        getInfo();
    }

    /**
     * 刷新首页数据啦
     */
    public void getInfo() {
        User user = Utils.getUserEntry();
        if (user != null) {
            HashMap<String, String> map = new HashMap<>();
            map.put("phoneId", user.getMobile());
//        map.put("password","123456");
            // TODO: 2017/8/15
            NetWorkUtils.post(MainActivity.this, "getenglishdesinfo", map,false, new NetWorkUtils.IListener() {

                @Override
                public void onSuccess(String result, JSONObject resObj) {
                    LogUtils.i("XHX", "XHXSUCC:" + result);
                    mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 0);

                }

                @Override
                public void onError(String result, String code, String msg) {
                    LogUtils.i("XHX", "XHXERROR:" + result);
                    mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 0);


                }
            });
        }
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case REFRESH_COMPLETE:
                    // TODO: 2017/8/15
                    mSwipeLayout.setRefreshing(false);
                    break;

            }
        }

    };
}
